package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.models.auth.In;
//import jdk.internal.org.jline.utils.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import static com.sky.constant.RedisConstant.REDIS_KEY_SHOP_STATUS;

/**
 * 管理端商家状态控制器
 */
@Slf4j
@RestController("shopAdminController")
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 设置营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status){
        String statusDesc = status == 0 ? "打烊中" : "营业中";
        log.info("[管理端]更新营业状态：{}",statusDesc);

        //获取String类型的操作接口实例
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        //更新数据到Redis中
        valueOperations.set(REDIS_KEY_SHOP_STATUS,String.valueOf(status));

        return Result.success();
    }

    /**
     * 查询营业状态
     * @return
     */
    @GetMapping("/status")
    public Result getStatus(){
        //获取String类型的操作接口实例
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        //从Redis中获取数据
        String statusStr = valueOperations.get(REDIS_KEY_SHOP_STATUS);
        Integer status = Integer.valueOf(statusStr);
        String statusDesc = status == 0 ? "打烊中" : "营业中";
        log.info("[管理端]获取营业状态：{}",statusDesc);

        //将获取到的数据返回给前端使用
        return Result.success(status);
    }
}
